//
//  KABasicAnimation.m
//  KAnimation
//
//  Created by Kien Tran on 5/31/13.
//  Copyright (c) 2013 TTK. All rights reserved.
//

#import "KABasicAnimation.h"
#import <QuartzCore/QuartzCore.h>
#define kKABasicAnimationStrengthWeak  0.1f
#define kKABasicAnimationStrengthStrong 0.25f
#define kKABasicAnimationStrengthNone 0.0f

@implementation KABasicAnimation


+ (void) shake:(UIView *)view withRingStrength:(eKABasicAnimationStrength)strength inDuration:(NSTimeInterval) duration withRepeat:(NSInteger)repeat withCompletionBlock:(void(^)())block {
        
    CGFloat strengthValue = 0;
    
    switch (strength) {
        case eKABasicAnimationStrengthNone:
            strengthValue = kKABasicAnimationStrengthNone;
            break;
        case eKABasicAnimationStrengthWeak:
            strengthValue = kKABasicAnimationStrengthWeak;
            break;
        case eKABasicAnimationStrengthStrong:
            strengthValue = kKABasicAnimationStrengthStrong;
            break;
    }
    
    [CATransaction begin];
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

    
    CGFloat toValue = M_PI * strengthValue;
    [anim setToValue:[NSNumber numberWithFloat:toValue]];
    
    [anim setFromValue:[NSNumber numberWithDouble:0.0f - toValue]];
    [anim setDuration:duration];
    [anim setRepeatCount:repeat >= 0 ? repeat : HUGE_VALF];
    [anim setAutoreverses:YES];
    if (block != nil) {
        [CATransaction setCompletionBlock:block];
    }
    [view.layer addAnimation:anim forKey:@"SpringboardShake"];
    [CATransaction commit];
}

+ (void) shake:(UIView *)view inDuration:(NSTimeInterval) duration withRepeat:(NSInteger)repeat {
    [self shake:view withRingStrength:eKABasicAnimationStrengthWeak inDuration:duration withRepeat:repeat withCompletionBlock:nil];
}
+ (void) shake:(UIView *)view withRingStrength:(NSInteger) strength inDuration:(NSTimeInterval) duration  withRepeat:(NSInteger)repeat {
    [self shake:view withRingStrength:strength inDuration:duration withRepeat:repeat withCompletionBlock:nil];
}

+ (void)shake:(UIView *)view withRingStrength:(NSInteger) strength {
    [self shake:view withRingStrength:strength inDuration:0.25f withRepeat:6];
}

+ (void)fadeIn:(UIView *)view inDuration:(NSTimeInterval) duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options withCompletionBlock:(void(^)(BOOL finished))block {
    if (view) {
        [view.layer setOpacity:0.0f];
        
        [UIView animateWithDuration:duration delay:delay options:options animations:^{
            [view.layer setOpacity:1.0f];
        } completion:block];
    }

}

+ (void)fadeIn:(UIView *)view inDuration:(NSTimeInterval) duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options {
    [self fadeIn:view inDuration:duration delay:delay options:options withCompletionBlock:nil];
}

+ (void)fadeIn:(UIView *)view inDuration:(NSTimeInterval) duration {
     [self fadeIn:view inDuration:duration delay:0.0f options:0 withCompletionBlock:nil];
}

+ (void)fadeOut:(UIView *)view inDuration:(NSTimeInterval) duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options withCompletionBlock:(void(^)(BOOL finished))block {
    if (view) {
        [view.layer setOpacity:1.0f];
        
        [UIView animateWithDuration:duration delay:delay options:options animations:^{
            [view.layer setOpacity:0.0f];
        } completion:block];
    }
    
}

+ (void)fadeOut:(UIView *)view inDuration:(NSTimeInterval) duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options {
    [self fadeIn:view inDuration:duration delay:delay options:options withCompletionBlock:nil];
}

+ (void)fadeOut:(UIView *)view inDuration:(NSTimeInterval) duration {
    [self fadeIn:view inDuration:duration delay:0.0f options:0 withCompletionBlock:nil];
}

+ (void)rotate360:(UIView *)view inDuration:(NSTimeInterval) duration delay:(NSTimeInterval)delay withLoopNumber:(NSInteger)loopNumber rightToLeft:(BOOL)isRightToLeft options:(UIViewAnimationOptions) options withCompletionBlock:(void(^)())block {
    if (view) {
        [CATransaction begin];
        
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        
      
        CGFloat toValue = M_PI * 2 * loopNumber;
        if (isRightToLeft) {
            [anim setToValue:[NSNumber numberWithFloat: 0 - toValue]];
            [anim setFromValue:[NSNumber numberWithDouble:view.transform.a]];

        } else {
            [anim setToValue:[NSNumber numberWithFloat:toValue]];
            [anim setFromValue:[NSNumber numberWithDouble:view.transform.a]];

        }
        
        [anim setDuration:duration];
        
        CGFloat repeat = loopNumber >= 0 ? 1 : HUGE_VALF;
        
        [anim setRepeatCount:repeat];
        
        if (block != nil) {
            [CATransaction setCompletionBlock:block];
        }
        [view.layer addAnimation:anim forKey:@"SpringboardShake"];
        [CATransaction commit];

    }
}

+ (void)rotate360:(UIView *)view inDuration:(NSTimeInterval) duration delay:(NSTimeInterval)delay withLoopNumber:(NSInteger)loopNumber rightToLeft:(BOOL)isRightToLeft options:(UIViewAnimationOptions) options {
    [self rotate360:view inDuration:duration delay:delay withLoopNumber:loopNumber rightToLeft:isRightToLeft options:options withCompletionBlock:nil];
}

+ (void)rotate360:(UIView *)view inDuration:(NSTimeInterval) duration withLoopNumber:(NSInteger)loopNumber rightToLeft:(BOOL)isRightToLeft {
    [self rotate360:view inDuration:duration delay:0.0f withLoopNumber:loopNumber rightToLeft:isRightToLeft options:0];
}

+ (void)rotate360:(UIView *)view withLoopNumber:(NSInteger)loopNumber {
    [self rotate360:view inDuration:3.0f*loopNumber delay:0.0f withLoopNumber:loopNumber rightToLeft:YES options:0];
}
@end
